<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<?php
if(!isset($_SESSION)){
	session_start();
}

if(!isset($_SESSION["adminName"]) || (empty($_SESSION["adminName"]))){
	header ("Location: ../index.php");
}
?>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Add Page</title>
    <link href="../../Styles/adminstype.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../js/admin_script.js"></script>
</head>
<body>
<?php
	$comboBox_LoaiSP = "";
	$comboBox_NhaXS = "";

	if(empty($_REQUEST["btnThem"]))//ko yeu cau them
	{
		// tạo combo Loai SP
		$width1 = 160;;
		$name1 ="LoaiSP";
		$active_1 = 0;
		$comboBox_LoaiSP = '<select style="width:'.$width1.'px" name="'.$name1.'" id="'.$name1.'" >'."\n";
		
		$sql = "select * from loaisanpham where BiXoa <> 1";
		require_once "../database/DataProvider.php";
		$result =  DataProvider::ExecuteQuery($sql);
		
		if(!empty($result)){
			while($row = mysql_fetch_array($result))
			{
				$comboBox_LoaiSP .= '<option value="'.$row["MaLoaiSanPham"].'"';
				if($active_1 == $row["MaLoaiSanPham"]) $comboBox_LoaiSP .= ' selected="selected" ';
				$comboBox_LoaiSP .= '>'.$row["TenLoaiSanPham"]."</option>\n";
			}
		}
		$comboBox_NhaXS .= "</select>\n";
		//tao conbo nha SX
		$width2 = 160;;
		$name2 ="NhaSX";
		$active_2 = 0;
		$comboBox_NhaXS = '<select style="width:'.$width2.'px" name="'.$name2.'" id="'.$name2.'" >'."\n";
		
		$sql = "select * from hangsanxuat where BiXoa <> 1";
		require_once "../database/DataProvider.php";
		$result =  DataProvider::ExecuteQuery($sql);
		
		if(!empty($result)){
			while($row = mysql_fetch_array($result))
			{
				$comboBox_NhaXS .= '<option value="'.$row["MaHangSanXuat"].'"';
				if($active_2 == $row["MaHangSanXuat"]) $comboBox_NhaXS .= ' selected="selected" ';
				$comboBox_NhaXS .= '>'.$row["TenHangSanXuat"]."</option>\n";
			}
		}
		$comboBox_NhaXS .= "</select>\n";
	}
?>
<table>
	<tr class="boxContentTitle">
		<td>Thêm sản phẩm</td>
	</tr>
	<tr>
		<td>
			 <form action="resource/form_add_product.php" method="post" name="form_add_product" enctype="multipart/form-data">
				<table class="tableBoxView">
					<tr>
						<td style="width: 200px"><td>
						<td style="width: 200px">Tên sản phẩm</td>
						<td><input type="text" maxlength="300" name="txtTen" id="txtTen" /></td>
					</tr>
					<tr>
						<td style="width: 200px"><td>
						<td style="width: 200px">Hình ảnh</td>
						<td><input type="file" name="image" id="image" /></td>
					</tr>
					<tr>
						<td style="width: 200px"><td>
						<td style="width: 200px">Giá sản phẩm</td>
						<td><input type="text" name="txtGia" id="txtGia" /></td>
					</tr>
					<tr>
						<td style="width: 200px"><td>
						<td style="width: 200px">Số lượng</td>
						<td><input type="text" maxlength="9" name="txtSoluong" id="txtSoluong" /></td>
					</tr>
					<tr>
						<td style="width: 200px"><td>
						<td style="width: 200px">Mô tả</td>
						<td><textarea style="height: 100px; width: 500px" name="txtMota" id="txtMota" /></textarea></td>
					</tr>
					<tr>
						<td style="width: 200px"><td>
						<td style="width: 200px">Mô tả chi tiết</td>
						<td><textarea style="height: 100px; width: 500px" name="txtMotaCT" id="txtMotaCT"></textarea></td>
					</tr>
					<tr>
						<td style="width: 200px"><td>
						<td style="width: 200px">Loại sản phẩm</td>
						<td><?php echo($comboBox_LoaiSP); ?></td>
					</tr>
					<tr>
						<td style="width: 200px"><td>
						<td style="width: 200px">Nhà sản xuất</td>
						<td><?php echo($comboBox_NhaXS); ?></td>
					</tr>
					<tr>
						<td><td>
						<td></td>
						<td><input type="submit" name="btnThem" value="Save" onclick="return kiemTraTxt3('txtTen','txtGia','txtSoluong')"/></td>
					</tr>
				</table>
			</form>
		</td>
	</tr>
	<tr>
		<td>
			<?php

			if(!empty($_REQUEST["btnThem"]))//có yeu cau them
			{
				$tenSP = $_REQUEST["txtTen"];
				$hinhAnh = "";
				$gia = $_REQUEST["txtGia"];
				$ngay = date("Y-m-d");
				$soLuong = $_REQUEST["txtSoluong"];
				$moTa = $_REQUEST["txtMota"];
				$moTaCT = $_REQUEST["txtMotaCT"];
				$loaiSP = $_REQUEST["LoaiSP"];
				$nhaSX = $_REQUEST["NhaSX"];
				if($tenSP != "") //kiểm tra 
				{
					if ($_FILES['image']['name'] != NULL)
					{
						$dir="../../images/";
						$file= $dir. basename($_FILES['image']['name']);
						if (move_uploaded_file($_FILES['image']['tmp_name'],$file)){
							$hinhAnh = $_FILES['image']['name'];
						}
					}
					
					//INSERT INTO `sanpham`(`MaSanPham`, `TenSanPham`, `HinhURL`, `GiaSanPham`, `NgayNhap`, `SoLuongTon`, `SoLuongBan`, `SoLuocXem`, `MoTa`, `MoTaChiTiet`, `BiXoa`, `MaLoaiSanPham`, `MaHangSanXuat`) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6],[value-7],[value-8],[value-9],[value-10],[value-11],[value-12],[value-13])
					$sql = "insert into sanpham(TenSanPham, HinhURL, GiaSanPham, NgayNhap, SoLuongTon, 
					SoLuongBan, SoLuocXem, MoTa, MoTaChiTiet, BiXoa, MaLoaiSanPham, MaHangSanXuat) 
					values('$tenSP', '$hinhAnh', $gia, '$ngay', $soLuong, 0, 0, '$moTa', '$moTaCT', 0, $loaiSP, $nhaSX)";
					echo($sql);
					require_once "../../database/DataProvider.php";
					$result =  DataProvider::ExecuteQuery($sql);
					echo($result);
				}
				header ("Location: ../admin_manager_product.php");
			}
			?>
		</td>
	</tr>
</table>
</body>
</html>